﻿using System;
using System.Collections.Generic;

using System.Linq;
using System.Web;
using System.Web.Mvc;

using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using HyEduOnline.Entities;
using HyEduOnline.Server.Teacher;
using HyEduOnline.Server.Admin;
using HyEduOnline.Server.Common;
using System.Data.Common;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using HyEduOnline.Models;
using WebSite.Areas.WebApi.Controllers;
//using Excel = Microsoft.Office.Interop.Excel;//ASP.NET中Excel的命名空间

namespace WebSite.Areas.WebApi.Controllers
{
    
    public class CommonDataImportController : Controller
    {
        private  int IsDeleMainData; //是否清除原主表数据
        private int IsAddUnExistData;//是否追加附表中不存的数据
        private string uploadPath = "~/upload/import/";
        private string errorPath = "~/upload/error/";
        private string CurrentFileName = "";
        

        /// <summary>
        /// 初始化获取数据库表名集放到dropdownlist里面
        /// </summary>
        public JsonResult init()
        {
            string[] tablesNameArray = new string[6]; //表名数组
            tablesNameArray[0] = "班级管理表";
            tablesNameArray[1] = "学院课程信息表";
            tablesNameArray[2] = "学院开课信息表";
            tablesNameArray[3] = "学生信息表";
            tablesNameArray[4] = "教师基本信息表";
            tablesNameArray[5] = "教师开课信息表";


            List<object> TableNameList = new List<object>();
            
            //(1)读取所有当前数据库中的表名
            try
            {
                Database db = DBHelper.CreateDataBase("PlatformData");


                DbCommand cmd = db.GetSqlStringCommand("select name from dbo.sysobjects where name='tb_DeanClass'or name='tb_DeanCourse'or name='tb_DeanOpenCourse'or name='tb_Teachers'or name='tb_TeaOpenCourse' or name='tb_Students' ORDER BY Name");

                using (DataSet myDs = db.ExecuteDataSet(cmd))
                {
                    DataTable tmpDT = myDs.Tables[0];
                    for (int i = 0; i < tmpDT.Rows.Count; i++)
                    {
                        tableName temptableName = new tableName();
                        string temp = ((DataRow)tmpDT.Rows[i])[0].ToString(); 
                        
                        temptableName.tableTrueName=temp;
                        temptableName.tableChineseName = tablesNameArray[i];
                        TableNameList.Add(temptableName);
                    }

                }
                return Json(TableNameList, JsonRequestBehavior.AllowGet);
            }
            catch (Exception e2)
            {

                return null;

            }
        }






        /// <summary>
        /// 第二步，上传Excel文件
        /// </summary>
        /// <param name="sender"></param>
        [HttpPost]
        public JsonResult btnExcelUploadClick(string qqfile)
        {
            const string FilePath = "/Upload/UploadExcel/";  //文件的保存路径

            string currentUserIDString = null;
            switch (Session["UserType"].ToString())
            {
                case "0":  //当前登录用户的类型为学生
                    currentUserIDString = frontCurrentStu.GetNo();
                    break;
                case "1": //当前登录用户的类型为教师
                    currentUserIDString = frontCurrentTea.GetNo();
                    break;
                case "2"://当前登录用户的类型为管理员
                    //Test11.14将GetUserNO改为GetSysUserName()
                    currentUserIDString = FrontUserInfo.GetSysUserName();
                    break;
                default:
                    break;
            }
            string fileName = DateTime.Now.ToString("ddHHmmssff");//名称
            CurrentFileName = fileName;
            string fileType = qqfile.Substring(qqfile.LastIndexOf("."));//类型
            string webFile = FilePath + currentUserIDString + "/UploadFile/" + fileName + fileType;//服务器端路径          

            string uploadPath = "";
            uploadPath = Server.MapPath(FilePath + currentUserIDString + "/UploadFile/");//物理存储路径
            Session["uploadPath"] = webFile;
            //若不存在当前目录则创建
            if (!Directory.Exists(uploadPath))
            {
                Directory.CreateDirectory(uploadPath);
            }
            //流存储
            using (var inputStream = Request.InputStream)
            {
                using (var flieStream = new FileStream(Server.MapPath(webFile), FileMode.Create))
                {
                    inputStream.CopyTo(flieStream);
                }
            }
            return Json(qqfile, JsonRequestBehavior.AllowGet);
        }

                /// <summary>
                /// 根据提交的数据进行导入
                /// </summary>//返回两个数据, 一个表示状态 出错地方,,仅仅在成功时表现插入的条数
                /// <param name="sender"></param>
                /// <param name="e"></param>
                ///   protected void btnImportDataClick(object sender, DirectEventArgs e)
                public JsonResult btnImportDataClick()
                {
                   
                    String StrErrorOrNot="";//表示错误原因或者正确导入时返回的值的相关信息
                    string DownLoadUrl = "";//下载地址
                    //(1)取导入表结构// Session["ImportTbStructs"] 
                    IList<ImportTableFieldStruct> list = (IList<ImportTableFieldStruct>)Session["ImportTbStructs"] ;//获取临时表
                  
                    //(2)读到数据,准备循环处理;
                    string targTableName = Session["TargetTable"].ToString();//目标表名
                    string uploadPath = Session["uploadPath"].ToString();//逻辑存储路径
                    string excelPath = Server.MapPath(uploadPath);
                    DataTable dt = null, dtSec = null;
                    if (!System.IO.File.Exists(excelPath))
                    {
                        return null;
                    }
                    try
                    {
                        dt = new RSExcelReader().getTableByExcelpath(excelPath);  //读取数据到表中，第一行为列的说明名,第二行为列名

                    }
                    catch (Exception e3)
                    {
                       
                        StrErrorOrNot = "出错了，读取Excel错误:" + e3.Message.ToString();
                        return Json(StrErrorOrNot, JsonRequestBehavior.AllowGet);
                    }



                    //(4)判断此数据是否合格;

                    DataTable dt_Error = dt.Clone();   //添加一个一样结构的table
                    DataSet dsSec;

                    dt_Error.Columns.Add("异常原因");
                    int I_faildCount = 0;
                    int I_TeaDetailsCount = dt.Rows.Count;
                    int ErrorClum = 0;
                    if (I_TeaDetailsCount < 2)
                    {
                        StrErrorOrNot = "出错了，读取Excel格式错误，行数太少!";
                        return Json(StrErrorOrNot, JsonRequestBehavior.AllowGet);
                    }

                    int dtCount = dt.Rows.Count;//总行数
                    int dtColCount = dt.Columns.Count;//总列数
                    int sucCount = 0;//成功导入数
                 
                    DbCommand cmd;
                    int targColCount = list.Count;//主目标表字段数(包括空)

                    
                    StringBuilder insertSQL = new StringBuilder(), fieldSQL = new StringBuilder(), valueSQL = new StringBuilder();

                    string insertSecSQL = "";





                    //INSERT INTO [tb_regstu] ([ID],[S_OtherID],[I_Sex],[S_IDCard],[S_AdmissionNo],[S_Name],[S_Gkbmh],[I_FirSpe],[I_SecSpe],[S_Mobile],[S_Addr],[S_Photo],[I_PayState],[I_PayFrom],[S_FromSchool],[S_Password],[D_BirDate],[I_SubjectID],[I_IsTp],[I_RegState],[I_ZzorPg],[I_WeiLei],[S_OtherKl]) 
                    //values (24,NULL,2,'500224199412268208','131070410374','李葉','13500117152217',5,7,'18223054023','自己到学院来拿','/pic/13500117152217.jpg',1,1,NULL,'19941026ly',NULL,1,1,1,2,NULL,NULL)

                    if (dtCount > 1)
                    {

                        try
                        {
                            Database db = DBHelper.CreateDataBase("PlatformData");

                            IsDeleMainData = int.Parse(Session["IsClearMainTable"].ToString());
                            //判断是否清除主表原有数据  test
                            if (IsDeleMainData == 1)  //前台勾选了清空原主表，设IsDeleMainData置为1，否则为0
                            {
                                //需要清空主表
                                 //将SelectedtargetTable代替 cmbMainTarget.SelectedItem.Value 
                                cmd = db.GetSqlStringCommand("delete from " + targTableName);

                                db.ExecuteNonQuery(cmd);//清空
                            }

                            int newID = 0;

                            //从第二行开始处理
                            for (int i = 1; i < dtCount; i++)
                            {
                                insertSQL.Clear();
                                fieldSQL.Clear();
                                valueSQL.Clear();
                                bool isFirstCol = true;//主表第一列，副表第一列
                                int I_failflag = 1;//1表示正确，0表示该行数据出错


                                try
                                {

                                    //将SelectedtargetTable代替 cmbMainTarget.SelectedItem.Value 
                                    insertSQL.Append("Insert into [" + targTableName + "](");//前字段名
                                    for (int j = 0; j < targColCount; j++)
                                    {
                                        ErrorClum = j;
                                        //(3)生成副表的InsertSQL//SecTableName目标表名
                                        if (!string.IsNullOrEmpty(list[j].SecTableName))
                                        {
                                            //如果有副表,由于副表每次只插入一列，所以不需要太多的处理


                                            //第一次先读，看看副表中是否已经有此列，如果有，直接返回id，不再插入
                                            //(3.1)如果存在多字段，则需要分割多段
                                            //SecFieldName表字段
                                            string[] secFNames = list[j].SecFieldName.Split('+');
                                            if (secFNames.Length > 1)
                                            {

                                                //(3.2)如果是多字段，则需要分割值

                                                string[] secValues = dt.Rows[i][j].ToString().Split('+');



                                                //(3.3)生成sql
                                                insertSecSQL = "select " + list[j].SoureFieldName + " from " + list[j].SecTableName + " where ";

                                                for (int iM = 0; iM < secFNames.Length; iM++)
                                                {
                                                    if (iM == 0)
                                                    {
                                                        insertSecSQL += secFNames[iM] + "= '" + secValues[iM].Trim() + "' ";
                                                    }
                                                    else
                                                    {
                                                        insertSecSQL += " and " + secFNames[iM] + "= '" + secValues[iM].Trim() + "' ";
                                                    }

                                                }
                                            }
                                            else
                                            {

                                                insertSecSQL = "select " + list[j].SoureFieldName + " from " + list[j].SecTableName + " where " + list[j].SecFieldName + "="
                                                    + getValueStringByType(dt.Rows[i][j].ToString(), list[j].SecFieldType);
                                            }



                                            cmd = db.GetSqlStringCommand(insertSecSQL);//查询副表
                                            dsSec = db.ExecuteDataSet(cmd);
                                            if (dsSec != null && dsSec.Tables.Count > 0 && dsSec.Tables[0].Rows.Count > 0)
                                            {
                                                newID = (int)dsSec.Tables[0].Rows[0][0];

                                            }
                                            else
                                            {
                                                //没有找到数据，可能需要追加//test1
                                                //是否允许追加副表数据?Session["IsAddUnExistData"]
                                                IsAddUnExistData = int.Parse(Session["IsAddUnExistData"].ToString());
                                                if (IsAddUnExistData == 1) //是否允许追加副表数据复选框被勾选，IsAddUnExistData的值为1，否则为0
                                                {
                                                    //没找到,需要追加数据

                                                    insertSecSQL = "Insert into [" + list[j].SecTableName + "](" + list[j].SecFieldName + ")values(";//第三方前字段名

                                                    insertSecSQL += getValueStringByType(dt.Rows[i][j].ToString(), list[j].SecFieldType) + ");select @@identity";


                                                    //返回新插入的主键


                                                    try
                                                    {
                                                        cmd = db.GetSqlStringCommand(insertSecSQL);




                                                        var tmpObj = db.ExecuteScalar(cmd);//导入副表,返回id
                                                        int.TryParse(tmpObj.ToString(), out newID);


                                                    }
                                                    catch (Exception Error2)
                                                    {

                                                        //将错误数据导入到临时dataTable中
                                                        I_failflag = 0;
                                                        DataRow ErrorRow = dt_Error.NewRow();
                                                        int coulumsCount = 0;
                                                        for (coulumsCount = 0; coulumsCount < dt.Columns.Count; coulumsCount++)
                                                        {
                                                            ErrorRow[coulumsCount] = dt.Rows[i][coulumsCount];
                                                        }
                                                        ErrorRow[coulumsCount] = Error2.Message.ToString() + "第 " + (j + 1) + " 列,列名:" + dt.Columns[j].ColumnName;
                                                      
                                                        dt_Error.Rows.Add(ErrorRow);
                                                        I_faildCount++;
                                                        break;
                                                    }
                                                }
                                                else
                                                {
                                                    //没有找到数据，需要写入出错数据记录
                                                    I_failflag = 0;
                                                    DataRow ErrorRow = dt_Error.NewRow();
                                                    int coulumsCount = 0;
                                                    for (coulumsCount = 0; coulumsCount < dt.Columns.Count; coulumsCount++)
                                                    {
                                                        ErrorRow[coulumsCount] = dt.Rows[i][coulumsCount];
                                                    }
                                                    ErrorRow[coulumsCount] = "出错了，不允许追加的情况下，第" + (j + 1) + "列，列名:" + dt.Columns[j].ColumnName + "无法写入，可能是数据不匹配！";
                                                    ErrorClum = 0;
                                                    dt_Error.Rows.Add(ErrorRow);
                                                   I_faildCount++;
                                                   break;
                                                }

                                            }
                                        }

                                        //(4)生成主表的InsertSQL

                                        if (!String.IsNullOrEmpty(list[j].TargetFieldName))
                                        {

                                            if (isFirstCol)
                                            {

                                                isFirstCol = false;//
                                            }
                                            else
                                            {  //当输入为空的时候
                                                if (dt.Rows[i][j].ToString() == "" || dt.Rows[i][j].ToString() == null)
                                                {
                                                    string ColumnName = dt.Columns[j].ColumnName;
                                                    string KeyWord = "必填";
                                                    int s = ColumnName.IndexOf(KeyWord);

                                                    if (ColumnName.IndexOf(KeyWord)>-1)
                                                    {

                                                        I_failflag = 0;
                                                        DataRow ErrorRow = dt_Error.NewRow();
                                                        int coulumsCount = 0;
                                                        for (coulumsCount = 0; coulumsCount < dt.Columns.Count; coulumsCount++)
                                                        {
                                                            ErrorRow[coulumsCount] = dt.Rows[i][coulumsCount];
                                                        }
                                                        ErrorRow[coulumsCount] = "第 " + (j + 1) + " 列,列名:" + dt.Columns[j].ColumnName + "出错！必填项不能为空";
                                                        ErrorClum = 0;
                                                        dt_Error.Rows.Add(ErrorRow);
                                                        I_faildCount++;
                                                        break;
                                                    }

                                                    continue;
                                                }
                                                fieldSQL.Append(",");
                                                valueSQL.Append(",");
                                            }

                                            if (list[j].TargetFieldName.Substring(0, 1) == "#")
                                            {
                                                //需要转换
                                                //需要字段名
                                                fieldSQL.Append(list[j].TargetFieldName.Substring(1));//去掉前面的@号，值为新的id
                                                valueSQL.Append(newID);
                                            }
                                            else
                                            {

                                               
                                                //需要字段名
                                                fieldSQL.Append(list[j].TargetFieldName);

                                                    valueSQL.Append(getValueStringByType(dt.Rows[i][j].ToString(), list[j].TargetFieldType));
                                            }

                                        }
                                    }
                                    insertSQL.Append(fieldSQL.ToString());
                                    insertSQL.Append(")Values(");//值数据

                                    insertSQL.Append(valueSQL.ToString());
                                    insertSQL.Append(")");//结束

                                    if (I_failflag == 1)
                                    {
                                        //写入主表
                                        cmd = db.GetSqlStringCommand(insertSQL.ToString());

                                        db.ExecuteNonQuery(cmd);//导入
                                        sucCount++;
                                    }

                                }
                                catch (Exception Error)
                                {

                                    //将错误数据导入到临时dataTable中
                                   
                                    DataRow ErrorRow = dt_Error.NewRow();
                                    int coulumsCount = 0;
                                    for (coulumsCount = 0; coulumsCount < dt.Columns.Count; coulumsCount++)
                                    {
                                        ErrorRow[coulumsCount] = dt.Rows[i][coulumsCount];
                                    }
                                    ErrorRow[coulumsCount] = Error.Message.ToString() + "第 " + (ErrorClum + 1) + " 列,列名:" + dt.Columns[ErrorClum].ColumnName;
                                   
                                    dt_Error.Rows.Add(ErrorRow);
                                    I_faildCount++;
                                    break;
                                   // I_Isfaild = 1;   //表明发生了异常
                                }


                            }


                        }
                        catch (Exception e2)
                        {
                            StrErrorOrNot ="出错了!,"+e2.Message.ToString();
                            return Json(StrErrorOrNot, JsonRequestBehavior.AllowGet);
                         
                        }
                        if (sucCount > 0)
                        {
                            StrErrorOrNot = "成功导入的数据" + sucCount + "条~!!";
                           
                        }
                        if (I_faildCount>0)
                        {
                            StrErrorOrNot+= ",其中错误数据" + dt_Error.Rows.Count + "条~,请下载编辑后重新导入";
                            DownLoadUrl = Printer(dt_Error);
                        }

                       

                    }

                    System.IO.File.Delete(excelPath);//删除上传文件
                    return Json(new { StrErrorOrNot, DownLoadUrl }, JsonRequestBehavior.AllowGet);

                }

                /// <summary>
                /// 根据字段类型，返回value
                /// </summary>
                /// <param name="value"></param>
                /// <param name="type"></param>
                /// <returns></returns>
                private string getValueStringByType(string value, string type)
                {
                    type = type.ToLower();
                    switch (type)
                    {
                        
                        case "int"://直接返回value

                            break;
                        case "numeric":
                            break;
                        case "float":
                            break;

                        default:
                            value = "'" + value + "'";

                            break;
                    }
                    return value;
                }



                


        /// <summary>
        /// 生成字段和列，并绑定数据源
        /// </summary>
        /// <param name="sourceFileName">文件名</param>
        /// <param name="targTableName">目标表名选中的表</param>
        /// <param name="_gp"></param>
        /// <param name="_store"></param>
                public JsonResult BindData()
                {
                    string StrError = null;//返回错误原因
                    //清除旧数据与记录集
                    //(1)读取目标表的所有列数据Session["uploadPath"]
                    string uploadPath = Session["uploadPath"].ToString();
                    string targTableName=Session["TargetTable"].ToString();//获取目标表
                    string myFiledsSql = @"SELECT syscolumns.name,systypes.name type,syscolumns.isnullable,
        syscolumns.length 
        FROM syscolumns, systypes 
        WHERE syscolumns.xusertype = systypes.xusertype 
        AND syscolumns.id = object_id('" + targTableName + "')";
                    IList<ImportTableFieldStruct> structList = new List<ImportTableFieldStruct>();
                    IList<ColumeStructs> colsList = new List<ColumeStructs>();

                    DataTable targetTableStru = null;//目标表表结构数据

                    //示例:
                    //name type isnullable length
                    // ID	int	0	4
                    //S_SubName	nvarchar	1	40
                    //I_ParentID	int	1	4
                    //S_Memo	nvarchar	1	40
                    //I_Level	int	1	4
                    //I_Sort	int	1	4
                    try
                    {
                        Database db = DBHelper.CreateDataBase("PlatformData");


                        DbCommand cmd = db.GetSqlStringCommand(myFiledsSql);

                        using (DataSet myDs = db.ExecuteDataSet(cmd))
                        {
                            targetTableStru = myDs.Tables[0];

                            for (int i2 = 0; i2 < targetTableStru.Rows.Count; i2++)
                            {
                                ColumeStructs colsItem = new ColumeStructs();
                                colsItem.ColName = targetTableStru.Rows[i2][0].ToString();
                                colsItem.ColType = targetTableStru.Rows[i2][1].ToString();
                                colsItem.ColNullable = targetTableStru.Rows[i2][2].ToString();
                                colsItem.ColLength = targetTableStru.Rows[i2][3].ToString();

                                colsList.Add(colsItem);//DataTable属性
                            }

                        }
                    }
                    catch (Exception e2)
                    {
                        StrError = "出错了!" + e2.Message.ToString();
                        return Json(new { structList, StrError }, JsonRequestBehavior.AllowGet);
                    }




                    //(3)读入源数据的Excel文件;
                    string excelPath = Server.MapPath(uploadPath);
                    DataTable dt = null;
                    if (!System.IO.File.Exists(excelPath))
                    {
                        return null;
                    }
                    try
                    {
                        dt = new RSExcelReader().getTableByExcelpath(excelPath);  //读取数据到表中，第一行为列的说明名,第二行为列名

                    }
                    catch (Exception e3)
                    {
                        StrError = "出错了，读取Excel错误:" + e3.Message.ToString();
                        return Json(new { structList, StrError }, JsonRequestBehavior.AllowGet);
                    }



                    //(4)判断此数据是否合格;

                    DataTable dt_Error = dt.Clone();   //添加一个一样结构的table
                    dt_Error.Columns.Add("异常原因");
                    int I_TeaDetailsCount = dt.Rows.Count;
                    if (I_TeaDetailsCount < 2)
                    {

                        StrError = "出错了，读取Excel格式错误，行数太少!";
                        return Json(new { structList, StrError }, JsonRequestBehavior.AllowGet);
                    }
                    //(5)读取有效行,并写入临时类

                   




                    //只处理第二列

                    DataRow sourcRow = dt.Rows[0];
                    int colCount = dt.Columns.Count;
                    for (int j = 0; j < colCount; j++)
                    {
                        ImportTableFieldStruct strutItem = new ImportTableFieldStruct();
                        //SourceFieldMem表字段说明
                        strutItem.SourceFieldMem = dt.Columns[j].ColumnName;//源表说明
                        string tmpField = sourcRow[j].ToString();//源表字段名
                        string[] myFields = tmpField.Split(';');//源表字段可以指定目标字段，以;号分隔

                        //查询与源表字段名相同的名称作为匹配字段，如果找不到，返回为空
                        strutItem.SoureFieldName = myFields[0];//
                        int taregIndex = CheckTareget(strutItem.SoureFieldName, targetTableStru);//taregIndex为对应的字段的所在行targetTableStru从数据库里面获取的字段
                        if (taregIndex > -1)
                        {
                            //找到了同名列，需要处理

                            strutItem.TargetFieldName = targetTableStru.Rows[taregIndex][0].ToString();
                            strutItem.TargetFieldType = targetTableStru.Rows[taregIndex][1].ToString();
                            strutItem.TargetFieldLength = targetTableStru.Rows[taregIndex][3].ToString();
                            strutItem.TargetFieldNullable = targetTableStru.Rows[taregIndex][2].ToString();
                        }

                        if (myFields.Length == 6)
                        {
                            //有第三方列
                            strutItem.TargetFieldName = myFields[1]; //返回的对应目标字段
                            strutItem.SecTableName = myFields[2];
                            strutItem.SecFieldName = myFields[3];
                            strutItem.SecFieldType = myFields[4].ToLower();//字段类型，小写
                            strutItem.SecFieldLength = myFields[5];
                        }
                        strutItem.GenID = (j + 1);


                        structList.Add(strutItem);

                    }
                    Session["ImportTbStructs"] = structList;//临时表
                    return Json(new { structList,StrError }, JsonRequestBehavior.AllowGet);
                }






                /// <summary>
                /// 检查sourceName与targettable中的列中是否一致，如果不一致，则返回-1，否则返回一致当的index
                /// </summary>
                /// <param name="SourceName"></param>
                /// <param name="targetName"></param>
                /// <returns></returns>
                private int CheckTareget(string SourceName, DataTable targetName)
                {
                    int returnValue = -1;
                    SourceName = SourceName.Trim();//去掉多余的空格
                    int tmpCount = targetName.Rows.Count;
                    for (int i = 0; i < tmpCount; i++)
                    {
                        string myName = targetName.Rows[i][0].ToString().Trim();
                        if (myName == SourceName)
                        {
                            returnValue = i;
                            break;
                        }
                    }

                    return returnValue;
                }
        /// <summary>
        /// 判断是否清除主表原有数据
        /// </summary>
        /// <param name="IsChecked"></param>
        /// <returns></returns>
                public void ValidateIsClearMainTable(int IsChecked)
                {
                    Session["IsClearMainTable"] = IsChecked;
                }        
        /// <summary>
        /// 判断是否需要追加附表中不存在的数据
        /// </summary>
        /// <param name="IsChecked"></param>
         public void ValidateIsAddUnExistData(int IsChecked)
         {
                  
                    Session["IsAddUnExistData"]=IsChecked;
         }
        /// <summary>
        /// 获取目标表名
        /// </summary>
        /// <param name="TargetTable">目标表名</param>
        public void GetCurrentChosenTargetTable(string TargetTable)
        {
            
            Session["TargetTable"]=TargetTable;
        }
        #region 导出Excel操作

        public string Printer(DataTable Error)
        {

            const string FilePath = "/Upload/CommonUploadErrorFile/";  //文件的保存路径//Server.MapPath(FilePath);//物理存储路径
            string ErrorFile = "导入出错列表" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xls";
            string Path = Server.MapPath(FilePath) + ErrorFile;
            ExcelHelper.CreateExcel(Error, Path, ErrorFile);
            return FilePath + ErrorFile;
        }
        #endregion          
    }
}
